package com.mxpio.erp.technology.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;

import com.mxpioframework.jpa.query.Operator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.mxpio.erp.common.technology.entity.Item;
import com.mxpio.erp.common.technology.entity.WorkCenter;
import com.mxpio.erp.common.technology.service.WorkCenterService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

@Tag(name = "WorkCenterController", description = "工作中心数据接口")
@RestController
@RequestMapping("/erp/tech/workcenter/")
public class WorkCenterController {
	@Autowired
	private WorkCenterService workCenterSerivce;

	@GetMapping("list")
	@Operation(summary = "工作中心列表", description = "获取工作中心列表", method = "GET")
	public Result<List<WorkCenter>> list(Criteria criteria) throws UnsupportedEncodingException {
		List<WorkCenter> items = workCenterSerivce.list(WorkCenter.class, criteria);
		return Result.OK(items);
	}
	
	@GetMapping("page")
	@Operation(summary = "工作中心列表", description = "获取工作中心列表", method = "GET")
	public Result<Page<WorkCenter>> page(Criteria criteria, Integer pageSize, Integer pageNo) throws UnsupportedEncodingException {
		Pageable page = PageRequest.of(pageNo-1, pageSize);
		Page<WorkCenter> items = workCenterSerivce.listPage(WorkCenter.class, page, criteria);
		return Result.OK(items);
	}
	
	@GetMapping("list/{workCenterCode}")
	@Operation(summary = "根据workCenterCode获取工作中心", description = "根据workCenterCode获取工作中心", method = "GET")
	public Result<WorkCenter> getById(@PathVariable(name = "workCenterCode", required = true) String workCenterCode) {
		WorkCenter item = workCenterSerivce.getById(WorkCenter.class, workCenterCode);
		return Result.OK(item);
	}

	@GetMapping("listbyworkshopcode/{workShopCode}")
	@Operation(summary = "根据workShopCode车间编码获取工作中心列表", description = "根据workShopCode车间编码获取工作中心列表", method = "GET")
	public Result<List<WorkCenter>> listByWorkShopCode(@PathVariable(name = "workShopCode", required = true) String workShopCode) {
		Criteria criteria = Criteria.create().addCriterion("workShopCode", Operator.EQ, workShopCode);
		List<WorkCenter> item = workCenterSerivce.list(WorkCenter.class, criteria);
		return Result.OK(item);
	}

	@PostMapping("add")
	@Operation(summary = "新增工作中心", description = "新增工作中心", method = "POST")
	public Result<WorkCenter> add(@RequestBody WorkCenter workCenter) {
 		workCenterSerivce.save(workCenter);
		return Result.OK(workCenter);
	}
	
	@PutMapping("edit")
	@Operation(summary = "编辑工作中心", description = "编辑工作中心（全量）", method = "PUT")
	public Result<WorkCenter> edit(@RequestBody WorkCenter workCenter) {
		workCenterSerivce.update(workCenter);
		return Result.OK(workCenter);
	}
	
	@DeleteMapping("remove/{workCenterCode}")
	@Operation(summary = "删除工作中心", description = "删除工作中心", method = "DELETE")
	public Result<Item> remove(@PathVariable(name = "workCenterCode", required = true) String workCenterCode) {
		String workCenterCodes[] = workCenterCode.split(",");
		for(String key : workCenterCodes){
			workCenterSerivce.delete(WorkCenter.class, key);
		}
		return Result.OK();
	}
}
